<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\Table\ShopSelectTable;
use App\Admin\Repositories\Service;
use App\Models\Area;
use App\Models\CarBrand;
use App\Models\ServiceCategory;
use App\Models\ShopUser;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Validation\Rule;

class ServiceController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Service(['category','shop']), function (Grid $grid) {
            $grid->disableCreateButton();
            $grid->column('id')->sortable();
            $grid->column('shop.name','所属门店');
            $grid->column('category.name','所属分类');
            $grid->column('image')->image(null,50,50);
            $grid->column('title');
            $grid->column('subtitle');
            $grid->column('price');
            $grid->column('sort')->sortable();
            $grid->column('status')->radio(['正常','隐藏'],true)->label(['info','default']);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->filter(function (Grid\Filter $filter){
                $filter->panel()->expand(false);
                $filter->equal("id")->width(2);
                $filter->equal("shop_id","所属门店")->select(ShopUser::getOptions())->width(2);
                $filter->equal("cid","所属分类")->select(ServiceCategory::getOptions())->width(2);
                $filter->like("title")->width(2);
                $filter->like("subtitle")->width(3);
                $filter->between("price")->width(3);
                $filter->equal("status")->radio(['正常','隐藏'])->width(3);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Service(), function (Show $show) {
            $show->field('id');
            $show->field('image');
            $show->field('title');
            $show->field('subtitle');
            $show->field('price');
            $show->field('sort');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

//    /**
//     * Make a form builder.
//     *
//     * @return Form
//     */
//    protected function form()
//    {
//        admin_style(<<<CSS
//.tab-pane:nth-child(2)>.row>.col-md-6>.row>.col-md-8{
//    display: none !important;
//}
//.tab-pane:nth-child(2)>.row>.col-md-6>.row>.col-md-2{
//    flex: 0 0 45% !important;
//    max-width: 45% !important;
//}
//.tab-pane:nth-child(3)>.row>.col-md-2{
//    display: none !important;
//}
//.tab-pane:nth-child(3)>.row>.col-md-8{
//    flex: 0 0 100% !important;
//    max-width: 100% !important;
//}
//CSS);
//        return Form::make(new Service(), function (Form $form) {
//            $form->tab("基础信息",function (Form $form){
//                $form->column(6,function (Form $form){
//
//                    $id = $form->getKey();
//                    $message = ['unique' => '此服务项目已存在' ];
//                    $rules = [
//                        'required'
//                    ];
//                    if($id){
//                        $rules[] = Rule::unique('services','title')->ignore($id);
//                    }else{
//                        $rules[] = Rule::unique('services','title');
//                    }
//                    $form->text('title')->rules($rules,$message)->required();
//                    $form->text('subtitle');
//                    $form->selectTable('shop_id','所属门店')->title('选择门店')
//                        ->dialogWidth('60%')
//                        ->from(ShopSelectTable::make(['id' => $form->getKey()]))
//                        ->model(ShopUser::class,"id","name")->width(5)->required();
//                    $form->select('cid','所属分类')->options(ServiceCategory::getOptions())->width(5)->required();
//                    $form->number('sort')->default(0);
//                    $form->radio('status')->options(['正常','隐藏'])->default(0);
//                });
//                $form->column(6,function (Form $form){
//                    $form->image('image')->uniqueName()->autoUpload()->help("图片尺寸:118*90")->required();
//                    $typeList = CarBrand::getTypesList();
//                    foreach ($typeList as $key=>$type){
//                        $form->decimal("prices.".$key,$type."价格")->required();
//                    }
//                });
//            });
//            $form->tab("分成设置",function (Form $form){
//                $form->column(6,function (Form $form){
//                    $form->embeds("city_rate0","无区代理的市代理分成设置",function ($form){
//                        $form->rate("0","余额/微信支付-分销比例(%)")->default(0);
//                        $form->currency("1","会员卡-分销值(￥)")->symbol("￥")->default(0);
//                    });
//                    $form->embeds("city_rate1","有区代理的市代理分成设置",function ($form){
//                        $form->rate("0","余额/微信支付-分销比例(%)")->default(0);
//                        $form->currency("1","会员卡-分销值(￥)")->symbol("￥")->default(0);
//                    });
//                    $form->embeds("district_rate","区代理的分成设置",function ($form){
//                        $form->rate("0","余额/微信支付-分销比例(%)")->default(0);
//                        $form->currency("1","会员卡-分销值(￥)")->symbol("￥")->default(0);
//                    });
//
//                });
//                $form->column(6,function (Form $form){
//                    $form->embeds("shop_rate","门店分成分成设置",function ($form){
//                        $form->rate("0","余额/微信支付-分销比例(%)")->default(0);
//                        $form->currency("1","会员卡-分销值(￥)")->symbol("￥")->default(0);
//                    });
//                    $form->embeds("worker_rate","技师师傅分成设置",function ($form){
//                        $form->rate("0","余额/微信支付-分销比例(%)")->default(0);
//                        $form->currency("1","会员卡-分销值(￥)")->symbol("￥")->default(0);
//                    });
//                    $form->embeds("user_rate","上级用户分销设置",function ($form){
//                        $form->rate("0","余额/微信支付-分销比例(%)")->default(0);
//                        $form->currency("1","会员卡-分销值(￥)")->symbol("￥")->default(0);
//                    });
//                });
//            });
////            $form->tab("地区设置",function (Form $form){
////                $cityList = Area::getOptions();
////                $options = $form->model()->areas?:[];
////                $typeList = CarBrand::getTypesList();
////                $form->html(view('admin.service.areas',[
////                    'cityList'=>$cityList,
////                    'typeList'=>$typeList,
////                    'options'=>$options,
////                ]));
////            });
//            $form->tab("图文介绍",function (Form $form){
//                $form->ckeditor("content");
//            });
//            $form->saving(function (Form $form){
//                if ($form->isCreating()||$form->isEditing()){
//                    $areas = request()->input("areas");
//                    $form->model()->areas = $areas?json_decode($areas,true):[];
//                }
//            });
//        });
//    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        admin_style(<<<CSS
.tab-pane:nth-child(2)>.row>.col-md-6>.row>.col-md-8{
    display: none !important;
}
.tab-pane:nth-child(2)>.row>.col-md-6>.row>.col-md-2{
    flex: 0 0 45% !important;
    max-width: 45% !important;
}
.tab-pane:nth-child(3)>.row>.col-md-2{
    display: none !important;
}
.tab-pane:nth-child(3)>.row>.col-md-8{
    flex: 0 0 100% !important;
    max-width: 100% !important;
}
CSS);
        return Form::make(new Service(['shop']), function (Form $form) {
            $form->tab("基础信息",function (Form $form){
                $form->column(5,function (Form $form){
                    $form->display("shop.name",'所属门店');
                    $form->image('image')->uniqueName()->autoUpload()->help("图片尺寸:118*90")->required();
                    $form->select('cid','所属分类')->options(ServiceCategory::getOptions())->width(5)->required();
                    $form->number('sort')->default(0);
                    $form->radio('status')->options(['正常','隐藏'])->default(0);
                });
                $form->column(7,function (Form $form){
                    $id = $form->getKey();
                    $message = ['unique' => '此服务项目已存在' ];
                    $rules = [
                        'required'
                    ];
                    $shop_id = $form->model()->shop_id;
                    if($id){
                        $rules[] = Rule::unique('services','title')->where("shop_id",$shop_id)->ignore($id);
                    }else{
                        $rules[] = Rule::unique('services','title')->where("shop_id",$shop_id);
                    }
                    $form->text('title')->rules($rules,$message)->required();
                    $form->text('subtitle');
                    $typeList = CarBrand::getTypesList();
                    foreach ($typeList as $key=>$type){
                        $form->decimal("prices.".$key,$type."价格")->required();
                        $form->decimal("member_prices.".$key,$type."会员价格")->required();
                    }
                });
            });
            $form->tab("洗车工分成设置",function (Form $form){
                $typeList = CarBrand::getTypesList();
                foreach ($typeList as $key=>$type){
                    $form->decimal("worker_rates.".$key,$type."价格对应的洗车工佣金")->required();
                }
            });
            $form->tab("图文介绍",function (Form $form){
                $form->ckeditor("content");
            });
            $form->saving(function (Form $form){
                if ($form->isCreating()||$form->isEditing()){
                    $areas = request()->input("areas");
                    $form->model()->areas = $areas?json_decode($areas,true):[];
                }
            });
        });
    }
}
